import React, { Component } from 'react'
import PropTypes from "prop-types"
import './index.css'
export default class index extends Component {
    state = {
        mouse: false
    }
    static propTypes = {
        updateTodo: PropTypes.func.isRequired,
        id:  PropTypes.string.isRequired,
        val: PropTypes.string.isRequired,
        remove: PropTypes.func.isRequired
    }
    handleMouse = (flag) => {
        return () => {
            this.setState({mouse: flag})
        }
    }
    removeTodo = (id) => {
        const {remove} = this.props;
        if(window.confirm("确定删除吗")) {
            remove(id)
        }
        
    }

    isChecked = (id) => {
        const {updateTodo} = this.props
        return (e) => {
            updateTodo(id,e.target.checked)
        }

    }

    render() {
        const {val,isComplete,id} = this.props
        const {mouse} = this.state
        return (
            <li style={{background: mouse ? "#ddd" : "#fff"}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" onChange={this.isChecked(id)} checked={isComplete}/>
                    <span>{val}</span>
                </label>
                <button className="btn btn-danger" onClick={() => this.removeTodo(id)} style={{ display:  mouse ? "block" : "none"}}>删除</button>
            </li>
        )
    }
}

